/**
 * Traffic.java
 * models the traffic and its atributes
 */
package br.unb.core.traffic;

import java.net.InetAddress;
import java.net.UnknownHostException;

import br.unb.plugins.geral.model.GeneratorModel;
import br.unb.utils.Console;
import br.unb.utils.Resource;

public class Traffic {

    /**Traffic identifier name*/
    private String name;
    /**Source IP address*/
    private InetAddress srcIp;
    /**Source port*/
    private int srcPort;
    /**Destination IP address*/
    private InetAddress dstIp;
    /**Destination port*/
    private int dstPort;
    /**Simulation start*/
    private int start;
    /**Simulation end*/
    private int duration;
    /**ToS : specifies type of service to be provided*/
    private String tos;
    /**Time between packets distribution, (exponetial, constant, etc)*/
    private String pauseType;
    /**Packet length distribution: Exponential or Fixed*/
    private String lengthType;
    /**Time generator, it is used to clock the time between two packets*/
    private GeneratorModel pauseModel;
    /**Length generator, it is used to distribute the packets sizes randomly*/
    private GeneratorModel lengthModel;
    /**Path to the file with data to fill the packets*/
    private String path;

    public Traffic() {
    }

    public Traffic(
            String nameTraffic,
            String srcIP,
            String srcPort,
            String dstIP,
            String dstPort,
            String startTime,
            String duration,
            String tos,
            String pauseType,
            GeneratorModel pauseGenerator,
            String lengthType,
            GeneratorModel lengthGenerator,
            String pathSrcPacket) throws IllegalArgumentException {
        super();
        this.name = nameTraffic;
        this.path = pathSrcPacket;
        this.tos = tos;
        this.pauseType = pauseType;
        this.pauseModel = pauseGenerator;
        this.lengthType = lengthType;
        this.lengthModel = lengthGenerator;

        int lineError = 0;
        try {
            this.dstPort = Integer.parseInt(dstPort);
        } catch (NumberFormatException nfe) {
            Console.showMessageDialog(
                    Resource.getString("INVALID_FIELD") + Resource.getString("DESTINATION_PORT"));
            lineError++;
        }
        try {
            this.srcPort = Integer.parseInt(srcPort);
        } catch (NumberFormatException nfe) {
            Console.showMessageDialog(
                    Resource.getString("INVALID_FIELD") + Resource.getString("SOURCE_PORT"));
            lineError++;
        }
        if (this.path == null || this.path.equals("")) {
            try {
                this.start = Integer.parseInt(startTime);
            } catch (NumberFormatException nfe) {
                Console.showMessageDialog(
                        Resource.getString("INVALID_FIELD") + Resource.getString("START"));
                lineError++;
            }
            try {
                this.duration = Integer.parseInt(duration);
            } catch (NumberFormatException nfe) {
                Console.showMessageDialog(
                        Resource.getString("INVALID_FIELD") + Resource.getString("DURATION"));
                lineError++;
            }
        } else {
            this.start = -1;
            this.duration = -1;
        }
        try {
            this.setSrcIP(InetAddress.getByName(srcIP));
        } catch (UnknownHostException e) {
            Console.showMessageDialog(
                    Resource.getString("ADDRESS_NOT_FOUND") + ":" + Resource.getString("SOURCE_IP"));
            lineError++;
        }
        try {
            this.setDstIP(InetAddress.getByName(dstIP));
        } catch (UnknownHostException e) {
            Console.showMessageDialog(
                    Resource.getString("ADDRESS_NOT_FOUND") + ":" + Resource.getString("DESTINATION_IP"));
            lineError++;
        }

        if (lineError > 0) {
            throw new IllegalArgumentException(
                    Resource.getString("BUILD_FAILED"));
        }
    }

    public InetAddress getDstIP() {
        return dstIp;
    }

    public int getDstPort() {
        return dstPort;
    }

    public int getDuration() {
        return duration;
    }

    public String getLengthType() {
        return lengthType;
    }

    public GeneratorModel getLengthModel() {
        return lengthModel;
    }

    public String getName() {
        return name;
    }

    public String getPath() {
        return path;
    }

    public GeneratorModel getPauseModel() {
        return pauseModel;
    }

    public String getPauseType() {
        return pauseType;
    }

    public InetAddress getSrcIP() {
        return srcIp;
    }

    public int getSrcPort() {
        return srcPort;
    }

    public int getStart() {
        return start;
    }

    public String getTos() {
        return tos;
    }

    public void setDstIP(InetAddress dIP) {
        this.dstIp = dIP;
    }

    public void setDstPort(int dP) {
        this.dstPort = dP;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public void setLengthType(String lengthType) {
        this.lengthType = lengthType;
    }

    public void setLengthModel(GeneratorModel lengthModel) {
        this.lengthModel = lengthModel;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setPauseModel(GeneratorModel pauseModel) {
        this.pauseModel = pauseModel;
    }

    public void setPauseType(String pauseType) {
        this.pauseType = pauseType;
    }

    public void setSrcIP(InetAddress sIP) {
        this.srcIp = sIP;
    }

    public void setSrcPort(int sP) {
        this.srcPort = sP;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public void setTos(String tos) {
        this.tos = tos;
    }

    /**
     * Transforms the chossen Diffservice class to DSCP code number
     * to be put in the IP header.
     * +------+--+
     * |DHCP  |NU|
     * +------+--+ 
     */
    public int tosToIntDHCP(String tos) {
        if (this.tos.equals("EF")) {
            return 184;//DHCP 0x2e << 2 = 46*4
        } else if (this.tos.equals("AF1")) {
            return 40;//DHCP 0x0a << 2 = 10*4
        } else if (this.tos.equals("AF2")) {
            return 72;//DHCP 0x12 << 2 = 18*4
        } else if (this.tos.equals("AF3")) {
            return 104;//DHCP 0x1a << 2 = 26*4
        } else {
            return 0;
        }
    }
    /**Internal strings*/
    private static final String[] trafficStrings = {"Name: ", " Start: ", " Duration: "};

    /**
     * This method creates a String containing name, start time and end of traffic
     * Important: Does not create the xml or contain information on other parameters of the traffic
     * If you want is to create an xml that represents the entire object use getXml
     *
     * Method used to record some attributes of the traffic in .gra file to be used
     * in qos analisys
     */
    public String toString() {
        return Traffic.trafficStrings[0] + getName() + Traffic.trafficStrings[1] + getStart() + Traffic.trafficStrings[2] + getDuration();
    }

    /**
     * This method creates a traffic from a String that represents some traffic parameters
     * Important: This Does not initialize all the parameters and does not create the traffic from xml
     * it is used by qos charts that read data from a file.
     * To create a complete object from the xml use xml2Traffic
     *
     * Method used to erect a traffic containing some
     * parameters used when analyzing the graphs of qos from one .gra
     */
    public static Traffic fromString(String trafficString) {
        /* String exmaple*/
        String tmpName = trafficString.substring(
                trafficString.indexOf(Traffic.trafficStrings[0]) + Traffic.trafficStrings[0].length(),
                trafficString.indexOf(Traffic.trafficStrings[ 1]));

        String tmpStart = trafficString.substring(
                trafficString.indexOf(Traffic.trafficStrings[1]) + Traffic.trafficStrings[1].length(),
                trafficString.indexOf(Traffic.trafficStrings[ 2]));

        String tmpDuration = trafficString.substring(
                trafficString.indexOf(Traffic.trafficStrings[2]) + Traffic.trafficStrings[2].length());

        return new Traffic(
                //String nameTraffic,
                tmpName,
                //String srcIP,
                "127.0.0.1",
                //String srcPort,
                "5000",
                //String dstIP,
                "127.0.0.1",
                //String dstPort,
                "5001",
                //String startTime,
                tmpStart,
                //String duration,
                tmpDuration,
                //String tos,
                "BE",
                //String pauseType,
                null,
                //GeneratorModel pauseGenerator,
                null,
                //String lengthType,
                null,
                //GeneratorModel lengthGenerator
                null,
                //String pathSrcPacket
                null);
    }
}
